﻿<#
.Synopsis
   获取指定服务的启动方式
.DESCRIPTION
    获取指定服务的启动方式--More
#>
function Get-ServiceStartMode
{
    [CmdletBinding(DefaultParameterSetName='Parameter Set 1', 
                  SupportsShouldProcess=$true, 
                  PositionalBinding=$false,
                  HelpUri = 'http://www.microsoft.com/',
                  ConfirmImpact='Medium')]
    [OutputType([String])]
    Param
    (
        [Parameter(Mandatory = $true,Position=1 )]         #指定必填              
        [string]$serviceName=""                            
    )

    Begin
    {
    }
    Process
    {
       	(Get-WmiObject Win32_Service -filter "Name='$serviceName'").StartMode
    }
    End
    {
    }
}

###################################################################################################

<#
.Synopsis
   删除指定服务
.DESCRIPTION
   删除指定服务--More
#>
function Remove-Service
{
    [CmdletBinding(DefaultParameterSetName='Parameter Set 1', 
                  SupportsShouldProcess=$true, 
                  PositionalBinding=$false,
                  HelpUri = 'http://www.microsoft.com/',
                  ConfirmImpact='Medium')]
    [OutputType([String])]
    Param
    (
        [Parameter(Mandatory = $true,Position=1 )]         #指定必填              
        [string]$serviceName=""                            
    )

    Begin
    {
    }
    Process
    {
       	$service = Get-WmiObject -Class Win32_Service -Filter "Name='$serviceName'"
        #$serviceToRemove.StopService()
        $service.delete()
        Stop-Service $serviceName  #用这行将编辑为禁止;却依然处于运行状态的服务从列表中删除
        return "success"
    }
    End
    {
    }
}



###################################################################################################

<#
.Synopsis
   安装指定服务;如果已存在则先删除原有服务,再安装新的服务
.DESCRIPTION
   安装指定服务--More
#>
function Install-Service
{
    [CmdletBinding(DefaultParameterSetName='Parameter Set 1', 
                  SupportsShouldProcess=$true, 
                  PositionalBinding=$false,
                  HelpUri = 'http://www.microsoft.com/',
                  ConfirmImpact='Medium')]
    [OutputType([String])]
    Param
    (
        [Parameter(Mandatory = $true,Position=1 )]         #服务名              
        [string]$serviceName = "ServiceName",     
        [Parameter(Mandatory = $true,Position=1 )]         #二进制文件所在位置              
        [string]$binaryPath = "binaryPath"                        
    )

    Begin
    {
    }
    Process
    {
        #如果已存在该服务则先移除
        if (Get-Service $serviceName -ErrorAction SilentlyContinue)
        {
        # using WMI to remove Windows service because PowerShell does not have CmdLet for this
        $serviceToRemove = Get-WmiObject -Class Win32_Service -Filter "name='$serviceName'"
        $serviceToRemove.StopService()
        $serviceToRemove.delete()
        "service removed"
        }
        else
        {
        # just do nothing
        "service does not exists"
        }

        "installing service"
        #开始安装服务;可指定运行运行该服务的账号;咱先不用
        #creating credentials which can be used to run my windows service
        #$secpasswd = ConvertTo-SecureString "MyPa$$word" -AsPlainText -Force
        #$mycreds = New-Object System.Management.Automation.PSCredential (".\MyUserName", $secpasswd)
        #$binaryPath = "c:\myServiceBinaries\MyService.exe"
        # creating widnows service using all provided parameters
        New-Service -name $serviceName -binaryPathName $binaryPath -displayName $serviceName -startupType Automatic #-credential $mycreds

        "installation completed"
    }
    End
    {
    }
}